package twentytwenty;

import java.math.BigInteger;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-12-15 14:49
 * @Modified By:
 */
public class AddStrings_415 {
    public static void main(String [] args){
        String num1 = "123";
        String num2 = "231";
        System.out.println(addStrings1(num1, num2));
    }

    public static String addStrings(String num1, String num2) {
        BigInteger bigInteger = new BigInteger(num1);
        BigInteger bigInteger1 = new BigInteger(num2);
        BigInteger add = bigInteger.add(bigInteger1);
        return add.toString();
    }

    public static String addStrings1(String num1, String num2) {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int add = 0;
        StringBuffer ans = new StringBuffer();
        while (i >= 0 || j >= 0 || add != 0) {
            int x = i >= 0 ? num1.charAt(i) - '0' : 0;
            int y = j >= 0 ? num2.charAt(j) - '0' : 0;
            int result = x + y + add;
            ans.append(result % 10);
            add = result / 10;
            i--;
            j--;
        }
        // 计算完以后的答案需要翻转过来
        ans.reverse();
        return ans.toString();
    }
}
